#include "command.h"
#include "clientproxy.h"
#include "log.h"


std::string Request(json_t* body, std::string name, Command cid)
{
	if (json_object_set_new(body, "command", json_integer((int)cid)) == -1) {
		return "MAKE BODY失败!!!";
	}

	int status;
	ClientProxy::getInstance().send(name, body, status);
	if (body) {
		json_decref(body);
		body = NULL;
	}
	return ClientProxy::getInstance().recive(name);
}

void doHelp(std::vector<std::string> param, std::vector<std::string>& inputtext)
{
	std::string prompt = "Help Menu:\n";
	for (auto i = 0; i < sizeof(CMap) / sizeof(Cmd_Map); i++) {
		prompt += "\t"+CMap[i].name+"\t:"+CMap[i].desc+"\n";
	}

	inputtext.push_back(prompt);
}

void doLs(std::vector<std::string>, std::vector<std::string>& inputtext)
{
	string res = "";
	//组装协议的body部分
	json_t* body = json_object();
	res = Request(body, "LS", Command::LS);

	inputtext.push_back(res);
}

void doClear(std::vector<std::string>, std::vector<std::string>& inputtext)
{
	inputtext.clear();
}

void doPlay(std::vector<std::string> param, std::vector<std::string>& inputtext)
{
	int vid = 0;
	vid = std::stoi(param[0]);
	string res = "";
	json_t* body = json_object();
	if(json_object_set_new(body, "vid", json_integer(vid))) {
		inputtext.push_back("vid error");
		return;
	}

	if (json_object_set_new(body, "command", json_integer((int)Command::PLAY)) == -1) {
		ALOGE("Make command 失败!!!");
	}else {
		int status;
		ClientProxy::getInstance().send("PLAY", body, status);
		if (body) {
			json_decref(body);
			body = NULL;
		}
	}

	//创建视频窗口
}

void doInfo(std::vector<std::string> param, std::vector<std::string>& inputtext)
{
	int vid = 0;
	vid = std::stoi(param[0]);
	string res = "";
	json_t* body = json_object();
	if(json_object_set_new(body, "vid", json_integer(vid))) {
		inputtext.push_back("vid error");
		return;
	}

	res = Request(body, "INFO", Command::INFO);
	inputtext.push_back(res);
}
